home *** CD-ROM | disk | FTP | other *** search
- Path: chronicle.mti.sgi.com!austern
- From: Valentin Bonnard <bonnardv@pratique.fr>
- Newsgroups: comp.std.c++
- Subject: Some ideas about C++ and a question
- Date: 04 Mar 1996 10:21:57 PST
- Organization: Internet Way
- Approved: austern@isolde.mti.sgi.com
- Message-ID: <4h9se5$m2n@s3.iway.fr>
- NNTP-Posting-Host: isolde.mti.sgi.com
- X-Original-Date: 2 Mar 1996 16:19:17 GMT
- X-Mailer: Mozilla 1.1N (Macintosh; I; 68K)
- X-Url: news:comp.std.c++#4h4oiv$4md@xanadu.io.com
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBVAwUBMTs04ky4NqrwXLNJAQEyIAH/eVr7vMtLcVaUGIMEbDSpc3KZSqZJu6IR
- x6+tgRRo9VKZYU81f8CNSY/7HbM/8vL5sVt2Nhsb2biWFsWd8JqK4w==
- =zNg4
- Originator: austern@isolde.mti.sgi.com
-
- I have 4 ideas about C++ and a question.
-
- 1) String literals
- ~~~~~~~~~~~~~~~~~~
- String literals should be const char* instead of char* (if it is not
- already the case).
-
-
- 2) Placement operators
- ~~~~~~~~~~~~~~~~~~~~~~
- class Object {
- void operator+ // or / or whatever
- (Object& result, const Object& b) const;
- void operator* (Object& result) const;
- Object operator* (const Object& result) const; // old one
- }
-
- should be called respectively by:
- a = b + c;
-
- and a = *b;
-
- and (old one) a*b;
-
- This will solve the [well-known] problem of copying the result into a temporary.
- The compatibility will be preserved since old syntax will be allowed to.
-
- 3) Use of explicit keyword
- ~~~~~~~~~~~~~~~~~~~~~~~~~~
- //========= Test.h =========
- class Base {
- public:
- virtual void VirtualFunc ();
- };
-
- class Derived : public Base {
- public:
- void VirtualFunc () { }
- };
-
- //========= Caller.cp =========
- #include <Test.h>
- class Derived2 : public Derived {
- public:
- void VirtualFunc ();
- };
-
- //========= Foo.cp =========
- #include <Test.h>
-
- void foo (explicit Derived* object)
- {
- object.VirtualFunc (); // nop (compiler does not generate code for this)
-
- Derived* AnyDerivedObject = object; // ok
- }
-
- void foo2 (Derived* object)
- {
- object.VirtualFunc (); // object can be Derived2
-
- explicit Derived* RealDerivedObject = object; // error
- }
-
- The meaning of explicit could be: while a derived class ptr [resp. ref] can be
- convert to a base class ptr [ref], it can't be converted to a base class
- explicit ptr [ref].
-
- So explicit would be a type qualifier; this usage is different with:
-
- > Rich Hickey
- > rich@rcs-hq.mhs.compuserve.com
-
- > I propose that the explicit keyword be allowed as a qualifier of a class
- > definition, such qualification taking the form of:
-
- > explicit class X{
- > //the definition of X
- > };
-
- Here, X cannot be derived but my Derived can be so I think my idea is better
- (please fell free comment/criticise this assertion).
-
-
- 4) Conversion of Type** to const Type**
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
- It's ok, this is an error; but why cannot Type** be converted to const Type*
- const* ?
-
- void foo ()
- {
- typedef int Type;
- Type i;
- const Type ci = 2;
- Type* pi = &i;
- Type** ppi = π
-
- const Type** ppci = ppi; // error because
- *ppci = &ci; // now pi == &ci
- *pi = 0; // and now we modify ci
-
- const Type*const* pcpci = ppi; // no problem because
- *pcpci = &ci; // this is impossible (since *pcpci is
- const)
- }
-
-
- Now a question:
-
- Will there be any way to redefine (overload) normal && or || on class ?
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
- I mean, now:
-
- class Int {
- public:
- operator int () const { return i; }
- operator && (int) const;
- operator || (int) const;
- Int (int I) : i(I) { }
-
- private:
- int i;
- };
-
- int Func ()
- {
- cout << "called\n";
- return 1;
- }
-
- void foo ()
- {
- int i (1);
- Int I (1);
-
- if (i || Func ()) // called is not printed
- ;
- if (I || Func ()) // called is printed
- ;
- }
-
- One could think that the standard should be change in order to support real
- overloaded operator && and ||.
-
- I can't see the point of doing that, so if it's the case for you to don't ask
- me nor criticise.
-
- Valentin Bonnard
- bonnardv@pratique.fr
- ---
- [ comp.std.c++ is moderated. To submit articles: Try just posting with your
- newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
- comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
- Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu
- ]
-